﻿using Art_To_Eagle.Lib.EagleLib.Structure;
using Newtonsoft.Json.Linq;
using System;

namespace Art_To_Eagle.Lib.EagleLib
{
    class Api
    {
        public const string BaseUrl = "http://localhost:41595";

        public static ApplicationInfo GetApplicationInfo()
        {
            try
            {
                var data = Utils.HttpGet(BaseUrl + "/api/application/info");
                if (!string.IsNullOrWhiteSpace(data))
                {
                    return new ApplicationInfo(data);
                }
                return null;
            }
            catch
            {
            }
            return null;
        }

        public static Folder CreateFolder(string name)
        {
            var param = new JObject
            {
                ["folderName"] = name
            };
            try
            {
                var data = Utils.HttpPost(BaseUrl + "/api/folder/create", param.ToString());
                var j = JObject.Parse(data);
                if ((string)j["status"] == "success")
                    return new Folder(j["data"]);
            }
            catch
            {
            }
            return null;
        }

        public static Folder RenameFolder(string id, string name)
        {
            var param = new JObject
            {
                ["folderId"] = id,
                ["folderName"] = name
            };
            try
            {
                var data = Utils.HttpPost(BaseUrl + "/api/folder/rename", param.ToString());
                var j = JObject.Parse(data);
                if ((string)j["status"] == "success")
                    return new Folder(j["data"]);
            }
            catch
            {
            }
            return null;
        }

        public static Folder[] GetFolderList()
        {
            try
            {
                var data = Utils.HttpGet(BaseUrl + "/api/folder/list");
                var j = JObject.Parse(data);
                if ((string)j["status"] == "success")
                {
                    var list = (JArray)j["data"];
                    var result = new Folder[list.Count];
                    for (int i = 0; i < list.Count; i++)
                        result[i] = new Folder(list[i]);
                    return result;
                }
            }
            catch
            {
            }
            return null;
        }

        public static Folder[] GetRecentFolderList()
        {
            try
            {
                var data = Utils.HttpGet(BaseUrl + "/api/folder/listRecent");
                var j = JObject.Parse(data);
                if ((string)j["status"] == "success")
                {
                    var list = (JArray)j["data"];
                    var result = new Folder[list.Count];
                    for (int i = 0; i < list.Count; i++)
                        result[i] = new Folder(list[i]);
                    return result;
                }
            }
            catch
            {
            }
            return null;
        }

        public static bool AddImageFromUrl(string url, string name, string website = null,
            string annotation = null, string[] tags = null, long modificationTime = -1, string folderId = null)
        {
            var param = new JObject
            {
                ["url"] = url,
                ["name"] = name,
                ["website"] = website,
                ["annotation"] = annotation,
                ["folderId"] = folderId,
                ["modificationTime"] = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()
            };
            if (tags != null)
                param["tags"] = JArray.FromObject(tags);
            if (modificationTime != -1)
                param["modificationTime"] = modificationTime;
            try
            {
                var data = Utils.HttpPost(BaseUrl + "/api/item/addFromURL", param.ToString());
                var j = JObject.Parse(data);
                return (string)j["status"] == "success";
            }
            catch
            {
            }
            return false;
        }

        public static bool AddImageFromUrl(WebImage image, string folderId = null)
            => AddImageFromUrl(image.Url, image.Name, image.Website, image.Annotation, image.Tags, image.ModificationTime, folderId);

        public static bool AddImageFromUrl(WebImage[] images, string folderId = null)
        {
            var param = new JObject
            {
                ["folderId"] = folderId,
                ["items"] = JArray.FromObject(images)
            };
            try
            {
                var data = Utils.HttpPost(BaseUrl + "/api/item/addFromURLs", param.ToString());
                var j = JObject.Parse(data);
                return (string)j["status"] == "success";
            }
            catch
            {
            }
            return false;
        }
    }
}
